package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class DateSalesStrategy implements SalesStrategy {
    private String date;

    public DateSalesStrategy(String date) {
        this.date = date;
    }

    @Override
    public double calculateSales(List<Order> orders) {
        return orders.stream()
                .filter(order -> order.getOrderTime().toString().contains(date))
                .mapToDouble(Order::getTotalPrice)
                .sum();
    }


    @Override
    public double calculateSalesFromDB(Connection connection) throws Exception {
        String query = "SELECT SUM(TotalPrice) AS TotalSales FROM Orders WHERE DATE(OrderTime) = ?";
        PreparedStatement statement = connection.prepareStatement(query);
        statement.setString(1, date);
        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()) {
            return resultSet.getDouble("TotalSales");
        }
        return 0;
    }
}
